package net.flydy.ywxcx.wxcloudrun.common;

import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;

/**
 * @author flydy
 */

@Slf4j
public class EncryptUtils {

    public static final int GCM_IV_LENGTH = 12;
    //16 = 128 bites
    public static final int GCM_TAG_LENGTH = 16;

    public static byte[] AesEncrypt(byte[] data, byte[] key, byte[] iv) {
        try {
            IvParameterSpec ivspec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), ivspec);
            return cipher.doFinal(data);
        } catch (Exception e) {
            log.error("", e);
            return null;
        }
    }

    public static byte[] AesDecrypt(byte[] data, byte[] key, byte[] iv) {
        try {
            IvParameterSpec ivspec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), ivspec);
            return cipher.doFinal(data);
        }catch (Exception e) {
            log.error("", e);
            return null;
        }
    }

    public static byte[] GCMEncrypt(byte[] plaintext, byte[] key, byte[] iv) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, gcmParameterSpec);
        byte[] results = cipher.doFinal(plaintext);
        return results;
    }

    public static byte[] GCMDecrypt(byte[] data, byte[] key, byte[] iv) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, gcmParameterSpec);
        byte[] results = cipher.doFinal(data);
        return results;
    }

    public static byte[] hashEncrypt(byte[] data, String algorithm) {
        try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            md.update(data);
            return md.digest();
        } catch (Exception never) {
            return null;
        }
    }

    /**
     * MAC算法可选以下多种算法
     *
     * <pre>
     * HmacMD5
     * HmacSHA1
     * HmacSHA256
     * HmacSHA384
     * HmacSHA512
     * </pre>
     */
    public static byte[] HMAC(byte[] data, byte[] key, String alg) throws Exception {
        SecretKey secretKey = new SecretKeySpec(key, alg);
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());
        mac.init(secretKey);
        return mac.doFinal(data);
    }

    public static byte[] md5(byte[] data) {
        return hashEncrypt(data, "MD5");
    }

}
